/**
 * jDreamer核心文件
 * @author 赵振华
 * 
 * jDreamer是可以开源的，面向对象的JavaScript富客户端框架，它提供了UI，提供了
 * 更方便的ajax交互
 * 当然，这一切，依靠了一个方便快速的JavaScript框架jQuery
 */
(function($){
	/**
	 * jDreamer对象
	 */
	var jDreamer = (function(){
		
		/**
		 * jDreamer对象的副本
		 * @param _target	打算在什么对象上面返回jDreamer对象	
		 */
		var jDreamer = function(_target){
			var jQobj = $(_target);
			//如果当前是一个jDreamer对象，那么返回。
			var jDreamerObject = jQobj.data("jDreamerObject");
			if(jDreamerObject){
				return jDreamerObject;
			}
			return undefined;
		};
		
		jDreamer.Html = {};
		
		//注册到window，使得jDreamer对象可以方便的被访问
		window._ = window.jDreamer = jDreamer;
	})();
})(jQuery);





/**
 * jDreamer全局配置
 * @author 赵振华
 * 提供一些配置选项，为程序运行提供一些必要的默认值
 */
(function(_,$){
	_.Config = {
		defaultDock : "#dock",	//默认的dock栏
		loadTimeout : 60000,
		layoutSelector : "div[changer='layout']",
		
		loggerType : undefined,	//可选值alert,ajax
		loggerUrl : undefined	//当loggerType为ajax时，要设置此url
	};
})(jDreamer,jQuery);





/**
 * 继承工具
 * @author 赵振华
 * 轻量级模拟继承原理。
 */
(function(_){
	_.extend = function(_this,_super){
		try{
			_super = new _super();
			for(var p in _super){
				if(p == "config"){
					if(_this[p] == undefined){
						_this[p] = {};
					}
					var newConfig = {};
					$.extend(newConfig,_this[p],_super[p]);
					_this[p] = newConfig;
				}else{
					_this[p] = _super[p];
				}
			}
			for(var p in _super.prototype){
				_this.prototype[p]=_super.prototype[p];
			}
		}catch(e){
			alert(e);
		}
	};
})(jDreamer);





/**
 * 日志记录器
 * @author 赵振华
 * 用于记录程序操作日记
 */
(function(_){
	_.Logger = function(_content,_parameter){
		var content = _content;
		if( _parameter != undefined ){
			for(i in _parameter){
				var p = _parameter[i];
				content = content.replace("{?}", p);
			}
		}
		
		var type = _.Config.loggerType;
		var url = _.Config.loggerUrl;
		if(type){
			var types = type.split(",");
			for(i in types){
				var t = types[i];
				if(t == "ajax"){
					$.post(url,{data:content});
				}else if(t == "alert"){
					alert(content);
				}
			}
		}
	};
})(jDreamer);





/**
 * Map对象
 * @author 赵振华
 */
(function( _ , $) {
	var Map = function() {
		//长度
		this.length = 0;
		//容器
		this.container = {};
	};
	Map.prototype.put = function(objName, objValue) {
		try {
			if (objValue && objName && objName != "") {
				if(this.get(objName) == undefined){
					this.length ++ ;
				}
				this.container[objName] = objValue;
			}
		} catch (e) {
		}
	};
	Map.prototype.get = function(objName) {
		try {
			if (this.container[objName])
				return this.container[objName];
		} catch (e) {
		}
	};
	Map.prototype.has = function(objValue) {
		try {
			for ( var p in this.container) {
				if (this.container[p] === objValue)
					return true;
			}
			return false;
		} catch (e) {
		}
	};
	Map.prototype.remove = function(objName) {
		try {
			if (this.container[objName]) {
				delete this.container[objName];
				this.length--;
			}
		} catch (e) {
		}
	};
	Map.prototype.pop = function(objName) {
		try {
			var ov = this.container[objName];
			if (ov) {
				delete this.container[objName];
				this.length--;
				return ov;
			}
			return null;
		} catch (e) {
		}
	};
	Map.prototype.removeAll = function() {
		try {
			this.clear();
		} catch (e) {
		}
	};
	Map.prototype.clear = function() {
		try {
			delete this.container;
			this.container = {};
			this.length = 0;
		} catch (e) {
		}
	};
	Map.prototype.isEmpty = function() {
		if (this.length === 0)
			return true;
		else
			return false;
	};
	Map.prototype.size = function() {
		return this.length ;
	};
	Map.prototype.call = function(fun) {
		try {
			if (fun != undefined){
				for ( var key in this.container) {
					var val = this.container[key];
					fun(val,key);
				}
			}
		} catch (e) {
		}
	};
	_.Map = Map;
})(jDreamer, jQuery);






/**
 * 工具函数
 * @author 赵振华
 */
(function(_, $) {
	_.Util = {

		/**
		 * 生产全球唯一码
		 * @returns {String}
		 */
		guid : function() {
			var guid = "";
			for ( var i = 1; i <= 32; i++) {
				var n = Math.floor(Math.random() * 16.0).toString(16);
				guid += n;
				if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
					guid += "-";
			}
			return guid;
		},
		browser : {
			ie:/msie/.test(window.navigator.userAgent.toLowerCase()),
			moz:/gecko/.test(window.navigator.userAgent.toLowerCase()),
			opera:/opera/.test(window.navigator.userAgent.toLowerCase()),
			safari:/safari/.test(window.navigator.userAgent.toLowerCase())
		}
	};
})(jDreamer, jQuery);





/**
 * ajax全局配置
 * @author 赵振华
 * 在这里配置Ajax的全局属性，事件监听，用于配合框架操作。
 */
(function(_,$){
	
	/**
	 * ajax全局配置
	 */
	$.ajaxSetup({
		timeout : 300000, // 请求超时时间（毫秒）,默认为5分钟
		cache : false,
		global : true	//触发全局
	});

	/**
	 * ajax全局事件
	 */
	$(document).ajaxSend(function(evt, request, settings) {
		//_.Mger.alert("开始请求 [ " + settings.url + " ]");
	});
	$(document).ajaxComplete(function(event, request, settings) {
		//_.Mger.alert("完成对 [ " + settings.url + " ] 的请求。");
	});
	$(document).ajaxError(function(event, xhr, settings, thrownError) {
		_.Mger.error("请求 [ " + settings.url + " ] 时发生错误。");
	});
	
})(jDreamer,jQuery);
/**
 * Ajax动画
 */
(function( _ , $ ){
	_.AnimateLayer = {
		number : 0,
		layer : null,
		open : function(){
			this.number ++ ;
			if(this.layer == null){
				this.layer = $("<div class='jdmer_animatelayer'><div class='loading_gif'></div></div>");
				this.layer.appendTo($("body"));
			}
			this.layer.fadeIn();
		},
		close : function(){
			this.number -- ;
			if(this.number == 0){
				this.layer.fadeOut();
			}
		}
	};
})(jDreamer,jQuery);





/**
 * 皮肤选择器
 */
(function(_,$){
	_.Skin = {
		current : "default",
		skinMap : new _.Map(),
		addSkin : function(_skin){	//添加皮肤
			var skin = {
				name : "default",	//皮肤名称
				css : [],	//css文件列表
				iehack : [],	//ie 处理css
				iefilter : []	//ie6 透明css文件
			};
			$.extend(skin,_skin);
			this.skinMap.put(skin.name , skin);
		},
		removeSkin : function(_name){
			this.skinMap.remove(_name);
		},
		chooser : function(_name,_call){
			_.AnimateLayer.open();
			var skin = this.skinMap.get(_name);
			if(skin){
				$.cookie('jdmer_skin_name',_name);
				this.current = _name;
				//删除之前的css
				$("link[jdmer='true']").remove();
				
				var head = $("head");
				for(i in skin.css){
					var file = skin.css[i];
					var jq = $("<link type='text/css' jdmer='true' href='"+file+"' rel='stylesheet'/>");
					jq.appendTo(head);
					jq.get(0).href = file;
				}
				
				if($.browser.msie){
					if($.browser.version < 8.0){
						for(i in skin.iehack){
							var file = skin.iehack[i];
							var jq = $("<link type='text/css' jdmer='true' href='"+file+"' rel='stylesheet'/>");
							jq.appendTo(head);
							jq.get(0).href = file;
						}
					}
					if($.browser.version == 6.0){
						for(i in skin.iefilter){
							var file = skin.iefilter[i];
							var jq = $("<link type='text/css' jdmer='true' href='"+file+"' rel='stylesheet'/>");
							jq.appendTo(head);
							jq.get(0).href = file;
						}
					}
				}
				if(_call) _call();	//回调
			}
			_.AnimateLayer.close();
		}
	};
})(jDreamer,jQuery);